package com.zr.subclass.controller;

import com.zr.subclass.meiju.StatusEnumSub;
import com.zr.subclass.pojo.*;
import com.zr.subclass.service.SubClassService;
import com.zr.util.ResultVo;
import com.zr.util.ResultVoBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Liyiding on 2019/9/22.
 */
@RestController
@CrossOrigin //支持跨域

public class SubClassController {
    @Autowired
    private SubClassService subClassService;

    @PostMapping("subClass/queryPage")//@RequestBody接受json格式，@Valid做表单验证， BindResult接受错误提示信息
    public ResultVo queryPage(@RequestBody SubClassSelectVo subClassSelectVo){
        ResultVo resultVo =  subClassService.queryPage(subClassSelectVo);
        return resultVo;
    }

    @PostMapping("subClass/add")//@RequestBody接受json格式，@Valid做表单验证， BindResult接受错误提示信息
    public ResultVo add(@RequestBody @Valid SubClassAddVo subClassAddVo, BindingResult bindingResult){
        //判断是否存在错误信息
        if (bindingResult.hasErrors()){
            //如果存在return给页面错误信息
            return ResultVoBuilder.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        //如果没有错误跳到service层进行逻辑处理
        ResultVo resultVo =  subClassService.add(subClassAddVo);
        return resultVo;
    }

    @PostMapping("subClass/updateStatus")//@RequestBody接受json格式，@Valid做表单验证， BindResult接受错误提示信息
    public ResultVo updateStatus(@RequestBody SubClassUpdateStatusVo subClassUpdateStatusVo, BindingResult bindingResult){
        //判断是否存在错误信息
        if (bindingResult.hasErrors()){
            //如果存在return给页面错误信息
            return ResultVoBuilder.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        //如果没有错误跳到service层进行逻辑处理
        ResultVo resultVo =  subClassService.updateStatus(subClassUpdateStatusVo);
        return resultVo;
    }

    @PostMapping("subClass/update")//@RequestBody接受json格式，@Valid做表单验证， BindResult接受错误提示信息
    public ResultVo update(@RequestBody @Valid SubClassUpdateVo subClassUpdateVo, BindingResult bindingResult){
        //判断是否存在错误信息
        if (bindingResult.hasErrors()){
            //如果存在return给页面错误信息
            return ResultVoBuilder.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        //如果没有错误跳到service层进行逻辑处理
        ResultVo resultVo =  subClassService.update(subClassUpdateVo);
        return  resultVo;
    }

}
